package yl.hs.bmipfull.controllers.equipment;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.equipment.DeviceRepairEntity;
import yl.hs.bmipfull.service.equipment.IDeviceRepairService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/*
 * DeviceRepairController
 * @category
 * @author 谢景广
 * @date 2022-11-15
 */
@Api(value = "设备维修信息操作接口", tags = "设备管理——设备维修信息接口")
@RestController
@RequestMapping("/api/equipment/repair")
public class DeviceRepairController extends ControllerBase {
    @ApiOperation(value = "根据查询条件获取设备维修信息的分页信息", notes = "获取设备维修的分页信息")
    @PostMapping("{rowNum}/{page}")
    public WebResult query(@PathVariable int rowNum, @PathVariable int page, @RequestBody PageQuery query) {
        try {
            PageResult<DeviceRepairEntity> result = repairService.queryPage(query, rowNum, page);

            svSvc.updateUserMenuView(query);
            return success("ok", result);
        }
        catch (Exception ex){
            return error(ex);
        }
    }

    @ApiOperation(value = "根据查询条件获取设备维修信息的分页信息", notes = "获取设备维修的分页信息")
    @PostMapping("/save")
    public WebResult save(@RequestBody SubmitModel model) {
        try {
            repairService.save(model);
            return success("保存成功");
        }
        catch (Exception ex){
            return error(ex);
        }
    }

    @ApiOperation(value = "删除设备维修信息", notes = "根据编号删除设备维修信息")
    @DeleteMapping("/delete/{sn}")
    public WebResult delete(@PathVariable("sn")String sn){
        try{
            var snArray = sn.split(",");
            repairService.delete(snArray);
            return success("删除成功");
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "获取设备维修信息", notes = "获取设备维修信息")
    @GetMapping("/get/{sn}")
    public WebResult get(@PathVariable("sn")String sn){
        try{

            return success("ok",repairService.findBySN(sn));
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    private IDeviceRepairService repairService;

    @Autowired
    public void setRepairService(IDeviceRepairService repairService) {
        this.repairService = repairService;
    }

    private ISearchViewService svSvc;

    @Autowired
    public void setSvSvc(ISearchViewService svSvc) {
        this.svSvc = svSvc;
    }
}
